//
//  TXViewController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/14.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "TXViewController.h"
#import "PassWordView.h"
#import "TXSucceedController.h"
#import "ChangePayPWController.h"

@interface TXViewController ()<PassWordViewDelegate>
@property (strong, nonatomic) PassWordView * PassView;
@property (weak, nonatomic) IBOutlet UILabel *TypeLabel;
@property (weak, nonatomic) IBOutlet UITextField *PriceText;
@property (weak, nonatomic) IBOutlet UILabel *PriceLabel;

@property (strong, nonatomic) NSString * PayType;

@end

@implementation TXViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"可提现余额";
    self.PayType = @"";
    self.PriceLabel.text = [NSString stringWithFormat:@"当前可提现余额%@元",self.AllPrice];
    
    self.PriceText.keyboardType = UIKeyboardTypeDecimalPad;
    
    self.PassView = [[NSBundle mainBundle] loadNibNamed:@"PassWordView" owner:self options:nil][0];
    self.PassView.delegate = self;
    self.PassView.frame = self.view.frame;
    [self.PassView CreatePassWordView];
    [self.view addSubview:self.PassView];
    self.PassView.hidden = YES;
    [self.PassView.PWTextField resignFirstResponder];
}


- (IBAction)ClickOnBtnToChooseType:(id)sender {
    
    UIAlertController * Alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择收款方式" preferredStyle:UIAlertControllerStyleActionSheet];
    [Alert addAction:[UIAlertAction actionWithTitle:@"支付宝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.PayType = @"Alipay";
        self.TypeLabel.text = @"支付宝";
        self.TypeLabel.textColor = KRGBColor(7, 160, 248);
        
    }]];
    
    [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:Alert animated:YES completion:nil];
}
- (IBAction)ClickOnBtnToAllPrice:(id)sender {
    
    self.PriceText.text = [NSString stringWithFormat:@"%@",self.AllPrice];
}
- (IBAction)ClickOnBtnToTXMoney:(id)sender {
    if (self.PayType.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请选择提现方式"];
        return;
    }
    if (self.PriceText.text.length == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写提现金额"];
        return;
    }
    if ([self.PriceText.text floatValue] == 0) {
        [MBProgressHUD showAlertViewWithText:@"请填写提现金额"];
        return;
    }
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [MainObjectWay PostWithMethod:@"get_paypwd" andAction:@"User" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的信息 = %@",responseObject);
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableDictionary * Datadic = responseObject[@"data"];
            
            if ([Datadic[@"pwd"] isEqual:@"0"]) {
                NSLog(@"未设置密码");
                UIAlertController * Alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂未设置密码" preferredStyle:UIAlertControllerStyleActionSheet];
                [Alert addAction:[UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    ChangePayPWController * changePW = [[ChangePayPWController alloc] init];
                    [self.navigationController pushViewController:changePW animated:YES];
                    
                }]];
                
                [Alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                [self presentViewController:Alert animated:YES completion:nil];
            }else{
                [self.PassView clearUpPassword];
                self.PassView.hidden = NO;
                [self.PassView.PWTextField becomeFirstResponder];
            }
            
        }
    } failure:^(NSError *error) {
        NSLog(@"错误信息 = %@",error);
    }];
    

}

- (void)PushPassWordWith:(NSString *)PWString{
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUseriPhone forKey:@"phone"];
    [dic setObject:self.PriceText.text forKey:@"pay_money"];
    [dic setObject:PWString forKey:@"pay_pwd"];
    [dic setObject:self.PayType forKey:@"pay_type"];
    
    [MainObjectWay PostWithMethod:@"withdraw" andAction:@"User" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"--%@",responseObject);
        [MBProgressHUD showAlertViewWithText:[NSString stringWithFormat:@"返回信息：%@",responseObject[@"msg"]]];
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            [self.PassView clearUpPassword];
            self.PassView.hidden = YES;
            [self.PassView endEditing:YES];
            TXSucceedController * txC = [[TXSucceedController alloc] init];
            [self.navigationController pushViewController:txC animated:YES];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [MBProgressHUD showAlertViewWithText:@"提交失败"];
    }];
   
}

- (void)MissMyPassWord{
    NSLog(@"忘记密码");
    ChangePayPWController * changeC = [[ChangePayPWController alloc] init];
    [self.navigationController pushViewController:changeC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
